﻿#if NET5_0_OR_GREATER
using System;

using zijian666.Converts.Abstractions;
using zijian666.Converts.Convertor.Base;
using zijian666.Converts.Core;
using zijian666.Converts.Extensions;

namespace zijian666.Converts.Convertor;

/// <summary>
/// <seealso cref="DateOnly" /> 转换器
/// </summary>
public class DateOnlyConvertor : FromConvertor<DateOnly>
                               , IFrom<object, DateOnly>
                               , IFrom<int, DateOnly>
                               , IFromTime<DateOnly>
{
    /// <inheritdoc/>
    public ConvertResult<DateOnly> From(IConvertContext context, DateTimeOffset input)
        => new(DateOnly.FromDateTime(input.LocalDateTime));

    /// <inheritdoc/>
    public ConvertResult<DateOnly> From(IConvertContext context, object input)
    {
        var r = context.Convert<DateTimeOffset>(input);
        if (r.Success)
        {
            return From(context, r.Value);
        }
        return context.ConvertFail(this, input, r.Exception);
    }

    /// <inheritdoc/>
    public ConvertResult<DateOnly> From(IConvertContext context, DateTime input) => new(DateOnly.FromDateTime(input));

    /// <inheritdoc/>
    public ConvertResult<DateOnly> From(IConvertContext context, int input) => new(DateOnly.FromDayNumber(input));

    /// <inheritdoc/>
    public ConvertResult<DateOnly> From(IConvertContext context, TimeOnly input) => context.ConvertFail(this, input);

    /// <inheritdoc/>
    public ConvertResult<DateOnly> From(IConvertContext context, DateOnly input) => new(input);

    /// <inheritdoc/>
    public ConvertResult<DateOnly> From(IConvertContext context, TimeSpan input) => context.ConvertFail(this, input);
}

#endif